package com.ruoyi.leave.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.leave.domain.WorkflowWaterMonthPlan;
import com.ruoyi.leave.domain.dto.WorkflowWaterMonthPlanDTO;
import com.ruoyi.leave.domain.vo.WorkflowWaterMonthPlanVO;
import com.ruoyi.leave.service.IWorkflowWaterMonthPlanService;
import com.ruoyi.manage.domain.dto.Plan;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.ruoyi.common.constant.CommonResult.DEFAULT_NUM;
import static com.ruoyi.common.constant.CommonResult.DEFAULT_PAGESIZE;

/**
 * 用水月度计划申请Controller
 *
 * @author ruoyi
 * @date 2024-01-04
 */
@Api(tags = "用水月度计划")
@RestController
@RequestMapping("/workFlow/waterMonthPlan")
public class WorkflowWaterMonthPlanController extends BaseController {
    @Autowired
    private IWorkflowWaterMonthPlanService workflowWaterMonthPlanService;
    @ApiOperation(value = "月度计划审核-分页查询",httpMethod = "GET")
    @GetMapping("/planReviewList")
    public TableDataInfo planReviewList(WorkflowWaterMonthPlanVO workflowWaterMonthPlanVO) {
        List<WorkflowWaterMonthPlanDTO> list = workflowWaterMonthPlanService.planReviewList(workflowWaterMonthPlanVO);
        if (StringUtils.isBlank(workflowWaterMonthPlanVO.getPageNum())) {
            workflowWaterMonthPlanVO.setPageNum(DEFAULT_NUM + "");
        }
        if (StringUtils.isBlank(workflowWaterMonthPlanVO.getPageSize())) {
            workflowWaterMonthPlanVO.setPageSize(DEFAULT_PAGESIZE + "");
        }
        list = list.stream().skip((Integer.parseInt(workflowWaterMonthPlanVO.getPageNum()) - 1) * Integer.parseInt(workflowWaterMonthPlanVO.getPageSize())).limit(Integer.parseInt(workflowWaterMonthPlanVO.getPageSize())).collect(Collectors.toList());
        return getDataTable(list);
    }

    @Log(title = "提交申请", businessType = BusinessType.UPDATE)
    @PostMapping("/updatePlanSP")
    public AjaxResult updatePlanSP(@RequestBody WorkflowWaterMonthPlan workflowWaterMonthPlan) {

        int i = workflowWaterMonthPlanService.insertWorkflowWaterMonthPlan(workflowWaterMonthPlan);
        if (0 == i) {
            return AjaxResult.error("月底计划记录不存在!!!");
        } else if (2 == i) {
            return AjaxResult.error("请勿重复提交!!!");
        }
        return toAjax(i);
    }

    /**
     * 通过申请
     */
    @Log(title = "通过申请", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/completePlanSP/{planId}")
    public AjaxResult completePlanSP(@PathVariable("planId") Long planId) {
        int i = workflowWaterMonthPlanService.completeTaskByPlanID(planId);
        return toAjax(i);
    }


    /**
     * 驳回申请
     */
    @Log(title = "驳回申请", businessType = BusinessType.UPDATE)
    @PostMapping("/rejectPlanSP")
    public AjaxResult rejectPlanSP(@RequestBody Plan plan) {
        int i = workflowWaterMonthPlanService.rejectTaskByPlanID(plan);
        return toAjax(i);
    }

    /**
     * 审批(农业)
     */
    @Log(title = "审批(农业)", businessType = BusinessType.UPDATE)
    @PostMapping("/examineAgriculturePlan")
    public AjaxResult examineAgriculturePlan(@RequestBody Plan plan) {
        int i = workflowWaterMonthPlanService.examineAgriculturePlan(plan);
        return toAjax(i);
    }

    /**
     * 审批(工业)
     */
    @Log(title = "审批(工业)", businessType = BusinessType.UPDATE)
    @PostMapping("/examineGyMonthPlan")
    public AjaxResult examineGyMonthPlan(@RequestBody Plan plan) {
        int i = workflowWaterMonthPlanService.examineGyMonthPlan(plan);
        return toAjax(i);
    }

}
